package com.faneui.transfer.gateway.bootup;

import static springfox.documentation.builders.PathSelectors.regex;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.filter.CharacterEncodingFilter;

import com.faneui.transfer.gateway.filter.SessionFilter;

import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
@ImportResource({ "classpath*:/spring/faneui*.xml" })
@EnableSwagger2
public class SpringBootSwaggerApplication implements EmbeddedServletContainerCustomizer {

	@Autowired
	private SessionFilter sessionFilter;

	public static void main(String[] args) {
		ConfigurableApplicationContext cac = SpringApplication.run(SpringBootSwaggerApplication.class, args);
		cac.registerShutdownHook();
	}

	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		registrationBean.setFilter(characterEncodingFilter);
		return registrationBean;
	}

	// @Bean
	// public FilterRegistrationBean securityFilter() {
	// FilterRegistrationBean registrationBean = new FilterRegistrationBean();
	// registrationBean.setFilter(new DelegatingFilterProxy("shiroFilter"));
	// registrationBean.addInitParameter("targetFilterLifecycle", "true");
	// registrationBean.addUrlPatterns("/*");
	// return registrationBean;
	// }
	@Bean
	public FilterRegistrationBean sesionFilter() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		registrationBean.setFilter(sessionFilter);
		registrationBean.addUrlPatterns("/users/*");
		return registrationBean;
	}

	// @Bean
	// public FilterRegistrationBean crossDomainFilter() {
	// FilterRegistrationBean registrationBean = new FilterRegistrationBean();
	// registrationBean.setFilter(new CrossDomainFilter());
	// registrationBean.addUrlPatterns("");
	// return registrationBean;
	// }

	@Bean
	public Docket swaggerSpringMvcPlugin() {
		ApiInfo apiInfo = new ApiInfo("exingcai users project", "users bootup", null, null, null, null, null);
		ApiSelectorBuilder asb = new Docket(DocumentationType.SWAGGER_2).select();
		asb.paths(regex("/users.*"));
		Docket docket = asb.build().apiInfo(apiInfo).useDefaultResponseMessages(false);
		return docket;
	}

	public void customize(ConfigurableEmbeddedServletContainer container) {
		// TODO Auto-generated method stub

	}

}
